package com.bobo.netty.client;

import com.bobo.netty.common.Constants;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@Service
public class ChatClient {
    @Resource
    private SimpleChatClientInitializer simpleChatClientInitializer;

    public static void main(String[] args) throws Exception {
        new ChatClient().run();
    }

    public void run() throws Exception {
        EventLoopGroup workerLoopGroup = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerLoopGroup);
            //2 设置nio类型的channel
            bootstrap.channel(NioSocketChannel.class);
            //3 设置监听端口
            bootstrap.remoteAddress("127.0.0.1", Constants.CHAT_PORT);
            //4 设置通道的参数
            bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
            bootstrap.handler(new ChatClientHandler());
            ChannelFuture f = bootstrap.connect();
            f.addListener((ChannelFuture futureListener) ->
            {
                if (futureListener.isSuccess()) {
                    System.out.println("客户端连接成功");
                } else {
                    System.out.println("客户端连接失败");
                }

            });
            Channel channel = f.channel();
            while(true){
                if(channel!=null && channel.isActive()){
                    ByteBuf buff = channel.alloc().buffer(64);
                    buff.writeByte(1);
                    ChannelFuture channelFuture = channel.writeAndFlush(buff);
                    channelFuture.addListener(new ChannelFutureListener() {
                        // write操作完成后调用的回调函数
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if(future.isSuccess()) { // 是否成功
                                System.out.println("客户端write操作成功");
                            } else {
                                System.out.println("客户端write操作失败");
                            }
                        }
                    });
                }else{
                    System.out.println("客户端已经断开连接");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerLoopGroup.shutdownGracefully();
        }

    }

    @Async("nettyServerThreadPool")
    public void sendMsg(Channel channel) {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("--------");
        String s = null;
        s = "啦啦啦啦";
        try {
            channel.writeAndFlush(s + "\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
